define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;

    var audioItem = require("./items/audioItem.html");
    var audioBlockTpl = require("./blocks/audioBlockTpl.html");

    var render_list = template.compile(audioBlockTpl);
    var render_item = template.compile(audioItem);


    var composeAudioView = require('./composes/composeAudioView.html');
    var audioView = require("./preview/audioView.html");
    var voicefooter = require("./footers/voicefooter.html")



    var emptyHtml = require('./emptys/voice.html')

    var audiofiles = function(element, layout) {
        this.element = $(element);
        this.layout = layout;
        this.selector = ".js_audio_container li.item:last";
        this.type = window.message_config.files_type_ids[2];
        this.init();
    }
    audiofiles.prototype.init = function() {
        this.element.empty();
        this.events();
        this.selectView();
        this.fileModel = {
            FileID: 0,
            Voice: {}
        }
        this.model = {}
    };
    audiofiles.prototype.templateData = function() {
        return {
            model: this.model,
            Voice: this.fileModel.Voice,
            app: window.message_config.app
        }
    };
    audiofiles.prototype.validation = function() {
         
        return true;
    };
    audiofiles.prototype.selectData = function(data) {
        var _self = this;
        $(".js_add_message").off("click").removeAttr('disabled').bind('click', function(event) {
            _self.generate(data)
        });
    }
    audiofiles.prototype.sync = function(data) {
        var _self = this;
        var preview = template.compile(audioView)(this.templateData());
        this.layout.showPreview();
        this.layout.setPreview(preview);
    }
    audiofiles.prototype.events = function() {
        var _self = this;
        $(document).off('click.js_audio_container').on("click.js_audio_container", ".js_audio_container li.item", function() {
            var $this = $(this),
                $fileId = $this.closest('li').data("fileId");
            $(".js_audio_container li.item").removeClass('selected');
            $this.addClass('selected').siblings().removeClass('selected');
            $fileId && _self.selectData(window.files[_self.type][$fileId]);

        })
    };
    audiofiles.prototype.selectView = function() {
        var $footer = $(voicefooter);
        var _self = this;
        var list_data = {};
        var uploadimte = null;
        $(".js_new_message", $footer).uploader({
            multi: true,
            url: window.message_config.upload_path,
            max_count: 100,
            data: {
                type: _self.type,
                data: window.message_config.upload_params
            },
            FilesAdded: function(up, files) {
                $.each(files, function(index, file) {
                    _self.list(false, file.id, file.name);
                });
            },
            UploadProgress: function(up, file) {
                var upfileid = "upload{0}".format(file.id);
                uploadimte = $("#{0}".format(upfileid));
                if (uploadimte.length) {
                    var percent = file.percent + "%";
                    $(".js_update_text", uploadimte).text(percent)
                    $(".js_process", uploadimte).width(percent)
                };

            },
            FileUploaded: function(up, file, res) {
                if ($.isPlainObject(res)) {
                    list_data[file.id] = res;
                } else {
                    uploadimte.closest('li').remove();
                    up.files.removeFile(file);
                    config.msg.info(res || config.lang.uplodError);
                }
            },
            UploadComplete: function(up, files) {
                var last_item = list_data[files[files.length - 1].id];
                last_item && _self.generate(last_item);
            }
        }, "voice")
        this.layout.setFooter($footer);
    }
    audiofiles.prototype.generate = function(data) {
        var _self = this;
        if (data) {
            this.fileModel.FileID = data.file_id;
            this.fileModel.Voice = data.Voice;
        };
        var warp = template.compile(composeAudioView)(this.templateData());
        var $warp = $(warp);


        var preview = template.compile(audioView)(this.templateData());

        this.layout.setWarp($warp);
        this.layout.showPreview();
        this.layout.setPreview(preview);
        this.layout.setMassfooter(this);

        this.layout.blcokDestroy()
    }
    audiofiles.prototype.list = function(data, file_id, file_name) {
        var rhtml = data ? render_item({
            list: data.file_item
        }) : render_item({
            ID: file_id,
            file_name: file_name
        });
        var sp = $('ul:last', this.element);
        if (sp.length) {
            sp.addhtml(rhtml, data)
        } else {
            if (data) {
                if (data.last_file_item.length) {
                    this.element.addhtml(render_list({
                        title: "最近",
                        file_items: render_item({
                            list: data.last_file_item
                        })
                    }), data);
                }
                if (data.file_item.length) {
                    this.element.append(render_list({
                        title: "全部",
                        file_items: rhtml
                    }));
                } else {
                    this.element.html(emptyHtml)
                }
            } else {
                $(".empty_placeholder_wrap").remove()
                this.element.append(render_list({
                    title: "全部",
                    file_items: rhtml
                }));
            }

        }

    };

    return audiofiles;

})
